package com.qiu.greened

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.qiu.greened.adapter.MainVpAdapter
import com.qiu.greened.databinding.ActivityMainBinding
import com.qiu.greened.ui.main.FollowFragment
import com.qiu.greened.ui.main.HomeFragment
import com.qiu.greened.ui.main.MyFragment
import com.qiu.greened.ui.main.NotificationsFragment
import com.qiu.greened.utils.BnvVp2Mediator
import com.qiu.greened.utils.SignTool

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    companion object {
        //4个Fragment对应的索引
        const val INDEX_FOLLOW = 0
        const val INDEX_HOME = 1
        const val INDEX_NOTIFICATIONS = 2
        const val INDEX_MY = 3
    }

    private val fragments = mapOf(
        INDEX_FOLLOW to FollowFragment(),
        INDEX_HOME to HomeFragment(),
        INDEX_NOTIFICATIONS to NotificationsFragment(),
        INDEX_MY to MyFragment()
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.apply {
            binding.vpMain.adapter = MainVpAdapter(this@MainActivity, fragments)
            //实现联动
            BnvVp2Mediator(binding.bnvMain, binding.vpMain) { _, vp2 ->
                vp2.isUserInputEnabled = true
            }.attach()
            vpMain.setCurrentItem(1, false)
        }
        SignTool.printSignatureMD5(this,"com.qiu.greened")
//        login()
    }

//    private fun login() {
//        lifecycleScope.launch {
//            val save = getSharedPreferences("data", MODE_PRIVATE)
//            val phone = save.getString("phone", "").toString()
//            if (phone != "") {
//                val password = save.getString("password", "").toString()
//                try {
//                    val data = ServiceCreator.create<UserService>()
//                        .getUserLoginData(UserLoginPostBean(phone, password)).dataConvert()
//                    save.edit {
//                        putString("userName", data.userName)
//                        putString("userAvatar", data.userAvatar)
//                        putString("token", data.token)
//                    }
//                    MyData.setLoginData()
//                    "登录成功".showToast()
//                } catch (e: Exception) {
//                    e.printStackTrace()
//                    "请求失败 ==> ${e.message}".showToast()
//                    Log.i("请求失败", "${e.message}")
//                }
//            }
//        }
//    }
}
